package com.chenlixian.fishandbearpaw.service.impl;

import com.chenlixian.fishandbearpaw.dao.ShareDao;
import com.chenlixian.fishandbearpaw.dao.UserDao;
import com.chenlixian.fishandbearpaw.entity.Dto.*;
import com.chenlixian.fishandbearpaw.entity.Vo.CommentVo;
import com.chenlixian.fishandbearpaw.entity.Vo.GeneralVO;
import com.chenlixian.fishandbearpaw.entity.Vo.MessageVo;
import com.chenlixian.fishandbearpaw.entity.Vo.ShareVo;
import com.chenlixian.fishandbearpaw.entity.enumEntity.ErrorListEnum;
import com.chenlixian.fishandbearpaw.service.ShareService;
import org.apache.tomcat.util.buf.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author chenlixian
 * @description TODO
 * @date 2020/3/18 11:52
 */
@Service
public class ShareServiceImpl implements ShareService {
    @Autowired
    private ShareDao shareDao;
    @Autowired
    private UserDao userDao;
    @Override
    public List<ShareVo> queryShare(ShareDto shareParam) {
        List<ShareDto> shareDto = shareDao.queryShare(shareParam);
        List<ShareImgDto> shareImgDtos = new ArrayList<>();
        for (ShareDto s : shareDto){
             shareImgDtos = shareDao.queryShareImg(s.getId());
             s.setShareImg(shareImgDtos);
             s.setTotal(shareImgDtos.get(0).getCount() + shareImgDtos.get(1).getCount());
        }
        List<ShareVo> shareVo = new ArrayList<>();
        for (ShareDto s1 : shareDto){
            ShareVo share = new ShareVo();
            share.setId(s1.getId());
            share.setContent(s1.getContent());
            share.setCreateTime(s1.getCreateTime());
            share.setHeadPic(s1.getHeadPic());
            share.setShareImg(s1.getShareImg());
            share.setTags(s1.getTags().split(","));
            share.setTotal(s1.getTotal());
            share.setUserName(s1.getUserName());
            share.setUserId(s1.getUserId());
            shareVo.add(share);
        }
        if (shareParam.getUserId() != null){
            for (ShareVo s2 : shareVo){
                if(shareDao.checkIsFavorite(shareParam.getUserId(),s2.getUserId()) == 1)
                    s2.setIsFavorite(1);
                else
                    s2.setIsFavorite(0);
            }
        }
        else {
            for (ShareVo s2 : shareVo){
                    s2.setIsFavorite(0);
            }
        }
        return shareVo;
    }

    @Override
    public Integer vote(String imgId,Integer userId,Integer opUserId) {
        Integer count = shareDao.vote(imgId);
        if (opUserId != userId)
            shareDao.addMessage(userId,opUserId,1);
        return count;
    }

    @Override
    public List<CommentVo> queryAllComment(String shareId) {
        return shareDao.queryAllComment(shareId);
    }

    @Override
    public Integer addComment(CommentDto commentDto) {
        String id = UUID.randomUUID().toString().replace("-","");
        commentDto.setId(id);
        Integer count = shareDao.addComment(commentDto);
        if (commentDto.getUserId() != commentDto.getAuthorId())
            shareDao.addMessage(commentDto.getAuthorId(),commentDto.getUserId(),3);
        return count;
    }

    @Override
    public GeneralVO create(CreateShareDto createShareDto) {
        String id = UUID.randomUUID().toString().replace("-","");
        String picId = null;
        ShareDto shareDto = new ShareDto();
        shareDto.setId(id);
        shareDto.setContent(createShareDto.getContent());
        shareDto.setUserId(createShareDto.getUserId());
        shareDto.setTags(StringUtils.join(createShareDto.getTags()));
        Integer count = shareDao.addShare(shareDto);
        ShareImgDto shareImgDto = new ShareImgDto();
        try {
            for (int i = 0; i < createShareDto.getPic().length; i++) {
                picId = UUID.randomUUID().toString().replace("-", "");
                shareImgDto.setId(picId);
                shareImgDto.setShareId(id);
                shareImgDto.setImgUrl(createShareDto.getPic()[i]);
                shareImgDto.setCount(0);
                shareDao.addShareImg(shareImgDto);
            }
        }
        catch (Exception ex){
            return new GeneralVO(ErrorListEnum.E20009,"发布失败");
        }
        if (count ==  1)
            return new GeneralVO(ErrorListEnum.E200,"发布成功");
        else
            return new GeneralVO(ErrorListEnum.E20009,"发布失败");
    }

    @Override
    public Integer queryMessage(Integer userId) {
        Integer count = shareDao.queryMessage(userId);
        return count;
    }

    @Override
    public Integer clearMessage(Integer userId) {
        Integer count = shareDao.clearMessage(userId);
        return count;
    }

    @Override
    public List<MessageVo> getMessage(Integer userId) {
        List<MessageDto> messageDtos = shareDao.getMessage(userId);
        List<MessageVo> messageVos = new ArrayList<>();
        for(MessageDto m : messageDtos){
            MessageVo messageVo = new MessageVo();
            messageVo.setUserId(m.getUserId());
            messageVo.setOpUserId(m.getOpUserId());
            messageVo.setCreateTime(m.getCreateTime());
            if (m.getOpType() == 1)
                messageVo.setOpType("投票");
            else if (m.getOpType() == 2)
                messageVo.setOpType("关注");
            else
                messageVo.setOpType("评论");
            messageVo.setUser(userDao.getUserById(m.getOpUserId()));
            messageVos.add(messageVo);
        }
        return messageVos;
    }
}
